package edu.ahpu.propertymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ahpu.propertymanage.dataclass.PageSet;
import edu.ahpu.propertymanage.pojo.PropertyTage;
import edu.ahpu.propertymanage.pojo.Visitor;
import edu.ahpu.propertymanage.service.PropertyTageService;
import edu.ahpu.propertymanage.service.VisitorService;
import edu.ahpu.propertymanage.util.GetID;
import edu.ahpu.propertymanage.util.TimeChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/visitor")
public class VisitorController {

    @Autowired
    VisitorService visitorService;

    @Autowired
    TimeChange timeChange;

    @Autowired
    GetID getID;

    @Autowired
    PropertyTageService propertyTageService;

    @PostMapping("visitorpage")
    public IPage<Visitor> visitorPage(@RequestBody PageSet pageSet) {
        IPage<Visitor> visitorIPage = new Page<>(pageSet.getCurrent(), pageSet.getSize());
        visitorIPage = visitorService.page(visitorIPage);
        for (Visitor v : visitorIPage.getRecords()) {
            v.setVisitorArrTime(timeChange.deleteTime(v.getVisitorArrTime()));
        }
        return visitorIPage;
    }

    @GetMapping("addvisitor")
    public String addVisitor() {
        return getID.createVisitorUUID();
    }

    @PostMapping("addvisitor")
    public String addVisitor(@RequestBody Visitor visitor) {
        visitor.setVisitorArrTime(timeChange.addTime(visitor.getVisitorArrTime()));
        boolean bool = visitorService.saveOrUpdate(visitor);
        if (bool)
            return "success-add-visitor";
        else
            return "error";
    }

    @PostMapping("detailvisitor")
    public Visitor detailVisitor(String visitorId) {
        Visitor visitor = visitorService.getById(visitorId);
        return visitor;
    }

    @PostMapping("deletevisitor")
    public String deleteVisitor(String visitorId) {
        System.out.println(visitorId);
        boolean bool = visitorService.removeById(visitorId);
        if (bool)
            return "success";
        else
            return "error";
    }

    //查找
    @PostMapping("searchvisitor")
    public List<Visitor> searchVisitor(String keyword) {
        //正则表达式验证是否为手机号
        String regex = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))[0-9]{8}$";
        //创建一个条件（为了条件查询语句）
        QueryWrapper<Visitor> wrapper = new QueryWrapper<>();
        //正则表达式查询结果
        boolean bool = keyword.matches(regex);
        //物业管理的表示，判断是否根据id查找
        PropertyTage propertyTage = propertyTageService.getById(1);
        //根据id查询
        if (keyword.length() >= 4 && keyword.substring(0, 4).equals(propertyTage.getTage())) {
            //sql语句(where VisitorId = keyword);
            wrapper.lambda().eq(Visitor::getVisitorId, keyword);
            List<Visitor> visitors = visitorService.list(wrapper);
            return visitors;
        } else if (bool) {
            //sql语句(where VisitorPhone = keyword);
            wrapper.lambda().eq(Visitor::getVisitorPhone, keyword);
            List<Visitor> visitors = visitorService.list(wrapper);
            return visitors;
        } else {
            //sql语句(where VisitorName = keyword);
            wrapper.lambda().eq(Visitor::getVisitorName, keyword);
            List<Visitor> visitors = visitorService.list(wrapper);
            return visitors;
        }
    }
}
